﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;

namespace TianlalaHSB.Utils
{
    /// <summary>
    /// ManagementObjectSearcher类：WMI应用信息
    /// WMI应用
    /// 1. 开始-运行-输入：wbemtest 回车
    /// 2. 单击"连接", 输入：root\cimv2 回车; 或者ROOT\SecurityCenter 
    /// 3. 单击"查询", 输入
    /// 操作系统:Win32_OperatingSystem,版本 Name
    /// 程序:Win32_Product;
    /// 进程PID:Win32_Process;
    /// BIOS信息:Win32_ComputerSystemProduct;
    /// 磁盘列表：Win32_LogicalDisk;
    /// 主板信息：Win32_BaseBoard;
    /// 光驱：Win32_CDROMDrive;
    /// BIOS：Win32_BIOS;
    /// 操作系统：Win32_OperatingSystem;
    /// 服务列表：Win32_Service;
    /// 内存：Win32_PhysicalMemory;
    /// 键盘信息：Win32_Keyboard;
    /// Modem：Win32_POTSModem;
    /// 计算机系统信息：Win32_ComputerSystem；设备名称：Name，机带RAM：TotalPhysicalMemory
    /// </summary>
    public class ManagementHelper
    {
        public static void GetManagement()
        {
            ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_ComputerSystem");
            foreach (ManagementObject mo in mos.Get())
            {
                // 输出属性值
                foreach (PropertyData pd in mo.Properties)
                {
                    Console.WriteLine(pd.Name + ": " + pd.Value);
                }
            }
        }

        /// <summary>
        /// 获取物理内存大小
        /// </summary>
        /// <returns></returns>
        public static long GetPhisicalMemory()
        {
            ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_ComputerSystem");
            foreach (ManagementObject mo in mos.Get())
            {
                if (mo["TotalPhysicalMemory"] != null)
                {
                    return Convert.ToInt64(mo["TotalPhysicalMemory"]);
                }
            }
            return 0;
        }


        //获取Windows的卷序列号
        static string GetVolumeSerialNumber(string driveLetter)
        {
            string query = "SELECT * FROM Win32_LogicalDisk WHERE DeviceID = '" + driveLetter.TrimEnd('\\') + "'";
            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
            {
                foreach (ManagementObject share in searcher.Get())
                {
                    // 注意：这里我们假设卷序列号在VolumeSerialNumber属性中，这取决于您的具体需求和环境
                    // 在某些情况下，您可能需要查询其他WMI类（如Win32_Volume）或使用其他方法
                    return share["VolumeSerialNumber"].ToString();
                }
            }
            return "Not Found"; // 如果没有找到，返回"Not Found"或其他适当的值
        }

        public static string GetMotherboardSerialNumber()
        {
            string query = "SELECT * FROM Win32_BaseBoard";
            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
            {
                foreach (ManagementObject share in searcher.Get())
                {
                    // 通常SerialNumber属性包含主板序列号
                    return share["SerialNumber"].ToString();
                }
            }
            return "Not Found";
        }

        public static string GetBIOSInformation()
        {
            string query = "SELECT * FROM Win32_BIOS";
            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
            {
                foreach (ManagementObject share in searcher.Get())
                {
                    // BIOS有多个属性，如Manufacturer, Version, ReleaseDate等
                    // 你可以根据需要获取它们
                    return $"Manufacturer: {share["Manufacturer"]}, Version: {share["Version"]}, ReleaseDate: {share["ReleaseDate"]}";
                }
            }
            return "Not Found";
        }
    }
}
